home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / util / tgrep20.zip / SEARCH.C < prev    next >
Text File  |  1994-04-02  |  12KB  |  453 lines

  1. /* search.c - searching subroutines using dfa, kwset and regex for grep.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.    Written August 1992 by Mike Haertel. */
  19.  
  20. #include <ctype.h>
  21. #include <limits.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <memory.h>
  25. #undef bcopy
  26. #define bcopy(s, d, n) memcpy((d), (s), (n))
  27.  
  28. #ifdef isascii
  29. #define ISALNUM(C) (isascii(C) && isalnum(C))
  30. #define ISUPPER(C) (isascii(C) && isupper(C))
  31. #else
  32. #define ISALNUM(C) isalnum(C)
  33. #define ISUPPER(C) isupper(C)
  34. #endif
  35.  
  36. #define TOLOWER(C) (ISUPPER(C) ? tolower(C) : (C))
  37.  
  38. #include "grep.h"
  39. #include "dfa.h"
  40. #include "kwset.h"
  41. #include "regex.h"
  42.  
  43. #define NCHAR (UCHAR_MAX + 1)
  44.  
  45. static void Gcompile(char *, size_t);
  46. static void Ecompile(char *, size_t);
  47. static char *EGexecute(char *, size_t, char **);
  48. static void Fcompile(char *, size_t);
  49. static char *Fexecute(char *, size_t, char **);
  50.  
  51. /* Here is the matchers vector for the main program. */
  52. struct matcher matchers[] = {
  53.   { "default", Gcompile, EGexecute },
  54.   { "grep", Gcompile, EGexecute },
  55.   { "ggrep", Gcompile, EGexecute },
  56.   { "egrep", Ecompile, EGexecute },
  57.   { "posix-egrep", Ecompile, EGexecute },
  58.   { "gegrep", Ecompile, EGexecute },
  59.   { "fgrep", Fcompile, Fexecute },
  60.   { "gfgrep", Fcompile, Fexecute },
  61.   { 0, 0, 0 },
  62. };
  63.  
  64. /* For -w, we also consider _ to be word constituent.  */
  65. #define WCHAR(C) (ISALNUM(C) || (C) == '_')
  66.  
  67. /* DFA compiled regexp. */
  68. static struct dfa dfa;
  69.  
  70. /* Regex compiled regexp. */
  71. static struct re_pattern_buffer regex;
  72.  
  73. /* KWset compiled pattern.  For Ecompile and Gcompile, we compile
  74.    a list of strings, at least one of which is known to occur in
  75.    any string matching the regexp. */
  76. static kwset_t kwset;
  77.  
  78. /* Last compiled fixed string known to exactly match the regexp.
  79.    If kwsexec() returns < lastexact, then we don't need to
  80.    call the regexp matcher at all. */
  81. static int lastexact;
  82.  
  83. void
  84. dfaerror(mesg)
  85.      char *mesg;
  86. {
  87.   fatal(mesg, 0);
  88. }
  89.  
  90. static void
  91. kwsinit(void)
  92. {
  93.   static char trans[NCHAR];
  94.   int i;
  95.  
  96.   if (match_icase)
  97.     for (i = 0; i < NCHAR; ++i)
  98.       trans[i] = TOLOWER(i);
  99.  
  100.   if (!(kwset = kwsalloc(match_icase ? trans : (char *) 0)))
  101.     fatal("memory exhausted", 0);
  102. }  
  103.  
  104. /* If the DFA turns out to have some set of fixed strings one of
  105.    which must occur in the match, then we build a kwset matcher
  106.    to find those strings, and thus quickly filter out impossible
  107.    matches. */
  108. static void
  109. kwsmusts(void)
  110. {
  111.   struct dfamust *dm;
  112.   char *err;
  113.  
  114.   if (dfa.musts)
  115.     {
  116.       kwsinit();
  117.       /* First, we compile in the substrings known to be exact
  118.      matches.  The kwset matcher will return the index
  119.      of the matching string that it chooses. */
  120.       for (dm = dfa.musts; dm; dm = dm->next)
  121.     {
  122.       if (!dm->exact)
  123.         continue;
  124.       ++lastexact;
  125.       if ((err = kwsincr(kwset, dm->must, strlen(dm->must))) != 0)
  126.         fatal(err, 0);
  127.     }
  128.       /* Now, we compile the substrings that will require
  129.      the use of the regexp matcher.  */
  130.       for (dm = dfa.musts; dm; dm = dm->next)
  131.     {
  132.       if (dm->exact)
  133.         continue;
  134.       if ((err = kwsincr(kwset, dm->must, strlen(dm->must))) != 0)
  135.         fatal(err, 0);
  136.     }
  137.       if ((err = kwsprep(kwset)) != 0)
  138.     fatal(err, 0);
  139.     }
  140. }
  141.  
  142. static void
  143. Gcompile(pattern, size)
  144.      char *pattern;
  145.      size_t size;
  146. {
  147. #ifdef __STDC__
  148.   const
  149. #endif
  150.   char *err;
  151.  
  152.   re_set_syntax(RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE);
  153.   dfasyntax(RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE, match_icase);
  154.  
  155.   if ((err = re_compile_pattern(pattern, size, ®ex)) != 0)
  156.     fatal(err, 0);
  157.  
  158.   dfainit(&dfa);
  159.  
  160.   /* In the match_words and match_lines cases, we use a different pattern
  161.      for the DFA matcher that will quickly throw out cases that won't work.
  162.      Then if DFA succeeds we do some hairy stuff using the regex matcher
  163.      to decide whether the match should really count. */
  164.   if (match_words || match_lines)
  165.     {
  166.       /* In the whole-word case, we use the pattern:
  167.      (^|[^A-Za-z_])(userpattern)([^A-Za-z_]|$).
  168.      In the whole-line case, we use the pattern:
  169.      ^(userpattern)$.
  170.      BUG: Using [A-Za-z_] is locale-dependent!  */
  171.  
  172.       char *n = malloc(size + 50);
  173.       int i = 0;
  174.  
  175.       strcpy(n, "");
  176.  
  177.       if (match_lines)
  178.     strcpy(n, "^\\(");
  179.       if (match_words)
  180.     strcpy(n, "\\(^\\|[^0-9A-Za-z_]\\)\\(");
  181.  
  182.       i = strlen(n);
  183.       bcopy(pattern, n + i, size);
  184.       i += size;
  185.  
  186.       if (match_words)
  187.     strcpy(n + i, "\\)\\([^0-9A-Za-z_]\\|$\\)");
  188.       if (match_lines)
  189.     strcpy(n + i, "\\)$");
  190.  
  191.       i += strlen(n + i);
  192.       dfacomp(n, i, &dfa, 1);
  193.     }
  194.   else
  195.     dfacomp(pattern, size, &dfa, 1);
  196.  
  197.   kwsmusts();
  198. }
  199.  
  200. static void
  201. Ecompile(pattern, size)
  202.      char *pattern;
  203.      size_t size;
  204. {
  205. #ifdef __STDC__
  206.   const
  207. #endif
  208.   char *err;
  209.  
  210.   if (strcmp(matcher, "posix-egrep") == 0)
  211.     {
  212.       re_set_syntax(RE_SYNTAX_POSIX_EGREP);
  213.       dfasyntax(RE_SYNTAX_POSIX_EGREP, match_icase);
  214.     }
  215.   else
  216.     {
  217.       re_set_syntax(RE_SYNTAX_EGREP);
  218.       dfasyntax(RE_SYNTAX_EGREP, match_icase);
  219.     }
  220.  
  221.   if ((err = re_compile_pattern(pattern, size, ®ex)) != 0)
  222.     fatal(err, 0);
  223.  
  224.   dfainit(&dfa);
  225.  
  226.   /* In the match_words and match_lines cases, we use a different pattern
  227.      for the DFA matcher that will quickly throw out cases that won't work.
  228.      Then if DFA succeeds we do some hairy stuff using the regex matcher
  229.      to decide whether the match should really count. */
  230.   if (match_words || match_lines)
  231.     {
  232.       /* In the whole-word case, we use the pattern:
  233.      (^|[^A-Za-z_])(userpattern)([^A-Za-z_]|$).
  234.      In the whole-line case, we use the pattern:
  235.      ^(userpattern)$.
  236.      BUG: Using [A-Za-z_] is locale-dependent!  */
  237.  
  238.       char *n = malloc(size + 50);
  239.       int i = 0;
  240.  
  241.       strcpy(n, "");
  242.  
  243.       if (match_lines)
  244.     strcpy(n, "^(");
  245.       if (match_words)
  246.     strcpy(n, "(^|[^0-9A-Za-z_])(");
  247.  
  248.       i = strlen(n);
  249.       bcopy(pattern, n + i, size);
  250.       i += size;
  251.  
  252.       if (match_words)
  253.     strcpy(n + i, ")([^0-9A-Za-z_]|$)");
  254.       if (match_lines)
  255.     strcpy(n + i, ")$");
  256.  
  257.       i += strlen(n + i);
  258.       dfacomp(n, i, &dfa, 1);
  259.     }
  260.   else
  261.     dfacomp(pattern, size, &dfa, 1);
  262.  
  263.   kwsmusts();
  264. }
  265.  
  266. static char *
  267. EGexecute(buf, size, endp)
  268.      char *buf;
  269.      size_t size;
  270.      char **endp;
  271. {
  272.   register char *buflim, *beg, *end, save;
  273.   int backref, start, len;
  274.   struct kwsmatch kwsm;
  275.   static struct re_registers regs; /* This is static on account of a BRAIN-DEAD
  276.                     Q@#%!# library interface in regex.c.  */
  277.  
  278.   buflim = buf + size;
  279.  
  280.   for (beg = end = buf; end < buflim; beg = end + 1)
  281.     {
  282.       if (kwset)
  283.     {
  284.       /* Find a possible match using the KWset matcher. */
  285.       beg = kwsexec(kwset, beg, buflim - beg, &kwsm);
  286.       if (!beg)
  287.         goto failure;
  288.       /* Narrow down to the line containing the candidate, and
  289.          run it through DFA. */
  290.       end = memchr(beg, '\n', buflim - beg);
  291.       if (!end)
  292.         end = buflim;
  293.       while (beg > buf && beg[-1] != '\n')
  294.         --beg;
  295.       save = *end;
  296.       if (kwsm.index < lastexact)
  297.         goto success;
  298.       if (!dfaexec(&dfa, beg, end, 0, (int *) 0, &backref))
  299.         {
  300.           *end = save;
  301.           continue;
  302.         }
  303.       *end = save;
  304.       /* Successful, no backreferences encountered. */
  305.       if (!backref)
  306.         goto success;
  307.     }
  308.       else
  309.     {
  310.       /* No good fixed strings; start with DFA. */
  311.       save = *buflim;
  312.       beg = dfaexec(&dfa, beg, buflim, 0, (int *) 0, &backref);
  313.       *buflim = save;
  314.       if (!beg)
  315.         goto failure;
  316.       /* Narrow down to the line we've found. */
  317.       end = memchr(beg, '\n', buflim - beg);
  318.       if (!end)
  319.         end = buflim;
  320.       while (beg > buf && beg[-1] != '\n')
  321.         --beg;
  322.       /* Successful, no backreferences encountered! */
  323.       if (!backref)
  324.         goto success;
  325.     }
  326.       /* If we've made it to this point, this means DFA has seen
  327.      a probable match, and we need to run it through Regex. */
  328.       regex.not_eol = 0;
  329.       if ((start = re_search(®ex, beg, end - beg, 0, end - beg, ®s)) >= 0)
  330.     {
  331.       len = regs.end[0] - start;
  332.       if (!match_lines && !match_words || match_lines && len == end - beg)
  333.         goto success;
  334.       /* If -w, check if the match aligns with word boundaries.
  335.          We do this iteratively because:
  336.          (a) the line may contain more than one occurence of the pattern, and
  337.          (b) Several alternatives in the pattern might be valid at a given
  338.          point, and we may need to consider a shorter one to find a word
  339.          boundary. */
  340.       if (match_words)
  341.         while (start >= 0)
  342.           {
  343.         if ((start == 0 || !WCHAR(beg[start - 1]))
  344.             && (len == end - beg || !WCHAR(beg[start + len])))
  345.           goto success;
  346.         if (len > 0)
  347.           {
  348.             /* Try a shorter length anchored at the same place. */
  349.             --len;
  350.             regex.not_eol = 1;
  351.             len = re_match(®ex, beg, start + len, start, ®s);
  352.           }
  353.         if (len <= 0)
  354.           {
  355.             /* Try looking further on. */
  356.             if (start == end - beg)
  357.               break;
  358.             ++start;
  359.             regex.not_eol = 0;
  360.             start = re_search(®ex, beg, end - beg,
  361.                       start, end - beg - start, ®s);
  362.             len = regs.end[0] - start;
  363.           }
  364.           }
  365.     }
  366.     }
  367.  
  368.  failure:
  369.   return 0;
  370.  
  371.  success:
  372.   *endp = end < buflim ? end + 1 : end;
  373.   return beg;
  374. }
  375.  
  376. static void
  377. Fcompile(pattern, size)
  378.      char *pattern;
  379.      size_t size;
  380. {
  381.   char *beg, *lim, *err;
  382.  
  383.   kwsinit();
  384.   beg = pattern;
  385.   do
  386.     {
  387.       for (lim = beg; lim < pattern + size && *lim != '\n'; ++lim)
  388.     ;
  389.       if ((err = kwsincr(kwset, beg, lim - beg)) != 0)
  390.     fatal(err, 0);
  391.       if (lim < pattern + size)
  392.     ++lim;
  393.       beg = lim;
  394.     }
  395.   while (beg < pattern + size);
  396.  
  397.   if ((err = kwsprep(kwset)) != 0)
  398.     fatal(err, 0);
  399. }
  400.  
  401. static char *
  402. Fexecute(buf, size, endp)
  403.      char *buf;
  404.      size_t size;
  405.      char **endp;
  406. {
  407.   register char *beg, *try, *end;
  408.   register size_t len;
  409.   struct kwsmatch kwsmatch;
  410.  
  411.   for (beg = buf; beg <= buf + size; ++beg)
  412.     {
  413.       if (!(beg = kwsexec(kwset, beg, buf + size - beg, &kwsmatch)))
  414.     return 0;
  415.       len = kwsmatch.size[0];
  416.       if (match_lines)
  417.     {
  418.       if (beg > buf && beg[-1] != '\n')
  419.         continue;
  420.       if (beg + len < buf + size && beg[len] != '\n')
  421.         continue;
  422.       goto success;
  423.     }
  424.       else if (match_words)
  425.     for (try = beg; len && try;)
  426.       {
  427.         if (try > buf && WCHAR((unsigned char) try[-1]))
  428.           break;
  429.         if (try + len < buf + size && WCHAR((unsigned char) try[len]))
  430.           {
  431.         try = kwsexec(kwset, beg, --len, &kwsmatch);
  432.         len = kwsmatch.size[0];
  433.           }
  434.         else
  435.           goto success;
  436.       }
  437.       else
  438.     goto success;
  439.     }
  440.  
  441.   return 0;
  442.  
  443.  success:
  444.   if ((end = memchr(beg + len, '\n', (buf + size) - (beg + len))) != 0)
  445.     ++end;
  446.   else
  447.     end = buf + size;
  448.   *endp = end;
  449.   while (beg > buf && beg[-1] != '\n')
  450.     --beg;
  451.   return beg;
  452. }
  453.